package com.telenav.comm;

import java.util.Hashtable;

import com.telenav.serverproxy.IServerProxyConstants;

public class HostProvider
{
    public final static String APP_URL = "http://telenav-90201.tnsoftware.telenav.com:8080/sunnyvale";

	public final static String API_KEY = "DCDFC0EC6EA35444565CF9D3F31516EA419850AE";
    
    public final static String MAP_API_URL = "http://api.map.baidu.com/geocoder?output=json&location=";   
	
	public final static String XMPP_SERVER_URL = "telenav-90040.tnsoftware.telenav.com";
    
    /**
     * Google API project id registered to use GCM.
     */
    public static final String SENDER_ID = "414693121859";

    /**
     * Intent used to display a message in the screen.
     */
    public static final String DISPLAY_MESSAGE_ACTION = "com.telenav.gcm.DISPLAY_MESSAGE";
    
	private Hashtable<String, String> actionAliasMap = new Hashtable<String, String>();
	
	public Host createHost(String protocol, String hostName, int port)
    {
        Host host = createHost();
        host.protocol = protocol;
        host.host = hostName;
        host.port = port;
        
        return host;
    }
    
	public void setServerConfig()
	{
		actionAliasMap.put(IServerProxyConstants.ACT_LOGIN, "");
		actionAliasMap.put(IServerProxyConstants.ACT_GET_CITY, MAP_API_URL);
	}
	
    protected Host createHost()
    {
        return new Host();
    }
    
    public String getUrl(Host host)
    {
        return host.getUrl();
    }
    
    public Host createHost(String action)
    {
    	if (null == action) 
    		return null;
    	
    	String actionURL = actionAliasMap.get(action);     
        Host host = createHostByURL(actionURL); 
        return host;
    }
    
    protected Host createHostByURL(String url)
    {
        if (url == null) return null;
        
        Host host = null;
        
        try
        {
            int index = url.indexOf("://");
            String networkType = url.substring(0, index).trim();
           
            int indexServer = url.indexOf("/", index + 3);
            String serverInfo;
            if (indexServer != -1)
            {
                serverInfo = url.substring(index + 3, indexServer);
            }
            else
            {
                serverInfo = url.substring(index + 3);
            }   
          
            int indexPort = serverInfo.indexOf(":");
            String server = serverInfo;
            String port   = null;
            
            if (indexPort != -1)
            {
                server = serverInfo.substring(0,indexPort).trim();
                port   = serverInfo.substring(indexPort+1).trim();
                
                indexPort = port.indexOf(":");
                if (indexPort != -1)
                {
                    port = port.substring(0,indexPort).trim();
                }
            }
            
            String webApp = "";
            String servletName = "";
            String file = "";
            if (indexServer != -1)
            { 
                String appInfo = url.substring(indexServer + 1);
                
                servletName = appInfo;
                index = appInfo.indexOf("/");
                if (index != -1)
                {
                    webApp = appInfo.substring(0,index);
                    servletName = appInfo.substring(index + 1);
                }
                
                if(webApp != null && webApp.length() > 0)
                {
                    file = "/" + webApp + "/" + servletName;
                }
                else
                {
                    file = "/" + servletName;
                }
            }
    
            String protocol = Host.HTTP;
            
            if(networkType.equalsIgnoreCase("SOCKET"))
            {
                protocol = Host.SOCKET;
            }
            
            host = this.createHost(protocol, server, port != null && port.length() > 0 ? Integer.parseInt(port) : 80);          
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
        
        return host;
        
    }
    
    public static String getImageUrl()
    {
    	return APP_URL + "/image";
    }
    
    public static String getHtmlUrl()
    {
    	return APP_URL + "/html";
    }
}
